package com.guli.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.JwtUtils;
import com.guli.commonutils.R;
import com.guli.commonutils.orderVo.UcenterMemberOrder;
import com.guli.eduService.client.UcenterClient;
import com.guli.eduService.entity.EduComment;
import com.guli.eduService.service.EduCommentService;
import com.guli.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author LMZovo
 * @since 2022-02-22
 */
@RestController
@RequestMapping("/eduservice/edu-comment")
public class EduCommentController {
    @Autowired
    private EduCommentService eduCommentService;
    @Autowired
    private UcenterClient ucenterClient;

    @ApiOperation(value = "根据课程id_分页查询课程评论的方法")
    @GetMapping("getCommentPage/{page}/{limit}")
    public R getCommentPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit,
                            String courseId) {
        Page<EduComment> commentPage = new Page<>(page, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        //判断课程id是否为空
        if (StringUtils.isEmpty(courseId)) {
            wrapper.eq("course_id", courseId);
        }
        //按最新排序
        wrapper.orderByDesc("gmt_create");
        //数据封装到commentPage中
        eduCommentService.page(commentPage, wrapper);

        List<EduComment> commentList = commentPage.getRecords();
        long current = commentPage.getCurrent();//当前页
        long size = commentPage.getSize();//一页记录数
        long total = commentPage.getTotal();//总记录数
        long pages = commentPage.getPages();//总页数
        boolean hasPrevious = commentPage.hasPrevious();//是否有上页
        boolean hasNext = commentPage.hasNext();//是否有下页

        HashMap<String, Object> map = new HashMap<>();
        map.put("current", current);
        map.put("size", size);
        map.put("total", total);
        map.put("pages", pages);
        map.put("hasPrevious", hasPrevious);
        map.put("hasNext", hasNext);
        map.put("list", commentList);

        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("/auth/addComment")
    public R addComment(HttpServletRequest request,
                        @RequestBody EduComment eduComment) {
        String memberid = JwtUtils.getMemberIdByJwtToken(request);
        //判断用户是否登录
        if (StringUtils.isEmpty(memberid)) {
            throw new GuliException(20001, "请先登录");
        }
        eduComment.setMemberId(memberid);
        //远程调用ucenter根据用户id获取用户信息
        UcenterMemberOrder memberVo = ucenterClient.getMemberInfoById(memberid);

        eduComment.setAvatar(memberVo.getAvatar());
        eduComment.setNickname(memberVo.getNickname());

        //添加评论
        eduCommentService.save(eduComment);

        return R.ok();
    }

}